//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.openea.eap.module.obpm.service.form.generator;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.Iterator;

import org.jsoup.nodes.Element;
import org.openbpm.base.core.util.AppUtil;
import org.openbpm.base.core.util.StringUtil;
import org.openbpm.base.core.util.ThreadMapUtil;
import org.openbpm.bus.api.constant.BusTableRelType;
import org.openbpm.bus.api.model.IBusTableRel;
import org.openbpm.bus.api.model.IBusinessColumn;
import org.openea.eap.module.obpm.model.form.design.FormColumn;
import org.openea.eap.module.obpm.model.form.design.FormGroup;
import org.springframework.stereotype.Component;

@Component
public class ElementUIFormElementGenerator extends AbsFormElementGenerator {
    public ElementUIFormElementGenerator() {
    }

    @Override
    public String getGeneratorName() {
        return "elementGenerator";
    }


    private void handleVModel(Element element, IBusinessColumn column) {
        element.attr("v-model", this.getModelPath(column));
        this.handleElementPlaceHolder(column, element);
    }

    @Override
    protected String getColumnOnetext(IBusinessColumn column) {
        Element element = this.getElement("el-input");
        this.handleVModel(element, column);
        this.handlePermission(element, column);
        this.handleValidateRules(element, column);
        return element.toString().concat(this.r(column));
    }

    private String r(IBusinessColumn column) {
        return this.a(column, (JSONArray) null);
    }

    private String a(IBusinessColumn column, JSONArray options) {
        Element element = this.getElement("span");
        element.attr("v-show", this.getPermissionPath(column) + "=='r'");
        element.attr("class", "readonly-text");
        JSONObject optionJson = new JSONObject();
        if (options != null) {
            for (int i = 0; i < options.size(); ++i) {
                JSONObject jsonObject = options.getJSONObject(i);
                optionJson.put(jsonObject.getString("key"), jsonObject.getString("txt"));
            }

            element.text("{{ " + this.getModelPath(column) + " | abFormat(" + optionJson.toJSONString() + ") }}");
        } else {
            element.text("{{ " + this.getModelPath(column) + " }}");
        }

        return element.toString();
    }

    @Override
    protected String getColumnDate(IBusinessColumn column) {
        Element element = this.getElement("ab-date");
        this.handleVModel(element, column);
        element.attr("v-bind:ab-permission", this.getPermissionPath(column));
        element.attr("desc", column.getTable().getComment() + "-" + column.getComment());
        element.attr("readonly-show-text", "true");
        JSONObject config = JSON.parseObject(column.getCtrl().getConfig());
        this.checkCtrConfig(column, config, (String) null);
        String format = config.getString("format");
        element.attr("format", format);
        return element.toString();
    }

    @Override
    protected String getColumnDic(IBusinessColumn column) {
        Element element = this.getElement("ab-dict");
        this.handleVModel(element, column);
        element.attr("v-bind:ab-permission", this.getPermissionPath(column));
        element.attr("desc", column.getTable().getComment() + "-" + column.getComment());
        element.attr("readonly-show-text", "true");
        JSONObject config = JSON.parseObject(column.getCtrl().getConfig());
        this.checkCtrConfig(column, config, "key");
        element.attr("dict-key", config.getString("key"));
        return element.toString();
    }

    @Override
    protected String getColumnIdentity(IBusinessColumn column) {
        Element element = this.getElement("ab-serial-no");
        this.handleVModel(element, column);
        element.attr("v-bind:ab-permission", this.getPermissionPath(column));
        this.handleValidateRules(element, column);
        JSONObject config = JSON.parseObject(column.getCtrl().getConfig());
        this.checkCtrConfig(column, config, "alias");
        element.attr("serial-no", config.getString("alias"));
        return element.toString().concat(this.r(column));
    }

    @Override
    protected String getColumnMultitext(IBusinessColumn column) {
        JSONObject config = JSON.parseObject(column.getCtrl().getConfig());
        IBusTableRel relation = (IBusTableRel) ThreadMapUtil.get("relation");
        Element element;
        if (config.getBooleanValue("isEditor")) {
            element = this.getElement("ab-editor");
            element.attr("v-bind:ab-permission", this.getPermissionPath(column));
            element.attr("desc", relation.getTableComment() + "-" + column.getComment());
        } else {
            element = this.getElement("el-input").attr("type", "textarea");
            this.handlePermission(element, column);
            this.handleValidateRules(element, column);
        }

        this.handleVModel(element, column);
        if (config.getInteger("eheight") != 0) {
            element.attr(":height", config.getString("eheight"));
        }

        if (config.getInteger("ewidth") != 0) {
            element.attr(":width", config.getString("ewidth"));
        }

        return config.getBooleanValue("isEditor") ? element.toString() : element.toString().concat(this.r(column));
    }

    @Override
    protected String getColumnCheckBox(IBusinessColumn column) {
        JSONObject config = JSON.parseObject(column.getCtrl().getConfig());
        this.checkCtrConfig(column, config, "options");
        Element checkbox = this.getElement("ab-checkbox-group");
        this.handleVModel(checkbox, column);
        this.handlePermission(checkbox, column);
        this.handleValidateRules(checkbox, column);
        JSONArray options = config.getJSONArray("options");

        for (int i = 0; i < options.size(); ++i) {
            JSONObject option = options.getJSONObject(i);
            Element checkboxEl = checkbox.appendElement("checkbox");
            checkboxEl.attr("label", option.getString("key"));
            checkboxEl.appendText(option.getString("txt"));
        }

        return checkbox.toString().concat(this.a(column, options));
    }

    @Override
    protected String getColumnRadio(IBusinessColumn column) {
        JSONObject config = JSON.parseObject(column.getCtrl().getConfig());
        this.checkCtrConfig(column, config, "options");
        Element radioGroup = this.getElement("radio-group");
        JSONArray options = config.getJSONArray("options");
        this.handleVModel(radioGroup, column);
        this.handlePermission(radioGroup, column);
        this.handleValidateRules(radioGroup, column);

        for (int i = 0; i < options.size(); ++i) {
            JSONObject option = options.getJSONObject(i);
            Element label = radioGroup.appendElement("radio");
            label.attr("label", option.getString("key"));
            label.appendText(option.getString("txt"));
        }

        return radioGroup.toString().concat(this.a(column, options));
    }

    @Override
    protected String getColumnSelect(IBusinessColumn column, Boolean isMultiple) {
        JSONObject config = JSON.parseObject(column.getCtrl().getConfig());
        this.checkCtrConfig(column, config, "options");
        Element iSelectEl = this.getElement("el-select");
        this.handleVModel(iSelectEl, column);
        this.handlePermission(iSelectEl, column);
        this.handleValidateRules(iSelectEl, column);
        iSelectEl.attr(":disabled", "'r'==" + this.getPermissionPath(column));
        if (isMultiple) {
            iSelectEl.attr("multiple", "true");
        }

        JSONArray options = config.getJSONArray("options");

        for (int i = 0; i < options.size(); ++i) {
            JSONObject option = options.getJSONObject(i);
            Element element = iSelectEl.appendElement("el-option");
            element.attr("value", option.getString("key"));
            element.text(option.getString("txt"));
        }

        return iSelectEl.toString().concat(this.a(column, options));
    }

    @Override
    protected String getColumnFile(IBusinessColumn column) {
        Element element = this.getElement("ab-upload");
        this.handleVModel(element, column);
        element.attr("v-bind:ab-permission", this.getPermissionPath(column));
        element.attr("desc", column.getTable().getComment() + "-" + column.getComment());
        return element.toString();
    }

    protected String n(IBusinessColumn column) {
        return "";
    }

    @Override
    protected String getColumnSignature(IBusinessColumn column) {
        Element element = this.getElement("ab-signature").attr("href", "javascript:void(0)").addClass("ivu-btn ivu-btn-primary");
        this.handleVModel(element, column);
        element.attr(":ab-permission", this.getPermissionPath(column));
        IBusTableRel relation = (IBusTableRel) ThreadMapUtil.get("relation");
        element.attr("desc", relation.getTableComment() + "-" + column.getComment());
        element.attr("column-name", column.getComment());
        element.appendText("签名");
        return element.toString();
    }

    @Override
    public String getSubAttrs(IBusTableRel rel) {
        if (rel.getType().equals(BusTableRelType.MAIN.getKey())) {
            return "";
        } else {
            StringBuilder sb = new StringBuilder();
            sb.append(" table-key=\"" + rel.getTableKey() + "\" ");
            if (rel.getType().equals(BusTableRelType.ONE_TO_MANY.getKey()) && !rel.getParent().getType().equals(BusTableRelType.MAIN.getKey())) {
                sb.append(" :visible.sync=\"subTempData." + rel.getBusObj().getKey() + "_" + rel.getTableKey() + "_Model\" ");
                sb.append(" append-to-body=\"true\" close-on-click-modal=\"false\" ");
            }

            return sb.toString();
        }
    }

    public String getSubAttrs(FormGroup group) {
        if (CollectionUtil.isEmpty(group.getColumnList())) {
            return "";
        } else {
            String tableKey = null;

            FormColumn column;
            for (Iterator var3 = group.getColumnList().iterator(); var3.hasNext(); tableKey = column.getTableKey()) {
                column = (FormColumn) var3.next();
                if (tableKey != null && !tableKey.equals(column.getTableKey())) {
                    return "";
                }
            }

            if (group.getTableRelation().getTableKey().equals(tableKey)) {
                return "";
            } else {
                return " table-key=\"" + tableKey + "\" ";
            }
        }
    }

    public Boolean i(IBusTableRel rel) {
        return rel.getType().equals(BusTableRelType.ONE_TO_MANY.getKey()) && !rel.getParent().getType().equals(BusTableRelType.MAIN.getKey()) ? true : false;
    }

    @Override
    protected void handlePermission(Element element, IBusinessColumn column) {
        element.attr("v-ab-permission:edit", this.getPermissionPath(column));
    }

    @Override
    protected void handleValidateRules(Element element, IBusinessColumn column) {
        super.handlePermission(element, column);
        element.attr("v-ab-validate", element.attr("ab-validate"));
        element.attr("desc", column.getTable().getComment() + "-" + column.getComment());
        element.removeAttr("ab-validate");
    }

    @Override
    public String getScopePath(IBusTableRel relation) {
        return ((VueFormElementGenerator) AppUtil.getBean(VueFormElementGenerator.class)).getScopePath(relation);
    }

    @Override
    protected String getPermissionPath(IBusinessColumn column) {
        return ((VueFormElementGenerator) AppUtil.getBean(VueFormElementGenerator.class)).getPermissionPath(column);
    }

    public String getLineColumn(IBusinessColumn column, IBusTableRel relation) {
        ThreadMapUtil.put("isLine", true);
        String str = this.getColumn(column, relation);
        ThreadMapUtil.remove("isLine");
        return str;
    }

    public String getLineColumn(FormGroup group, FormColumn formColumn) {
        IBusTableRel tableRel = group.getTableRelation();
        if (StringUtil.isNotEmpty(formColumn.getTableKey()) && !formColumn.getTableKey().equals(tableRel.getTableKey())) {
            IBusTableRel table = tableRel.find(formColumn.getTableKey());
            if (table != null) {
                tableRel = table;
            }
        }

        if (!formColumn.getTableKey().equals(tableRel.getTableKey())) {
            this.LOG.error("生成表单可能存在异常！formColumnTableKey{},tableRelTableKey{}", formColumn.getTableKey(), tableRel.getTableKey());
        }

        IBusinessColumn businessColumn = tableRel.getTable().getColumnByKey(formColumn.getKey());
        if (businessColumn == null) {
            this.LOG.error("布局模板查找Column配置失败！字段：{}，表：{}", formColumn.getComment(), formColumn.getTableKey());
        }

        return this.getLineColumn(businessColumn, tableRel);
    }
}
